import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author ChangLiang
 * @date 2020/12/21
 */
public class FileRenamer {

    public static void main(String[] args) {
        File file = new File("D:\\02_HFI\\80_discrete_mathematics\\");
        File[] files = file.listFiles();

        List<File> newFiles = new ArrayList<>(files.length);

        // 正则匹配掉视频名称中包含的短链
        for (File file1 : files) {
            String oldName = file1.getName();
            String newName = oldName.replaceAll("-(.*)\\.mp4", ".mp4");
            renameFileName(newName,file1,oldName);
            newFiles.add(file1);
        }

        String[] fileNames = {
                "044_谓词逻辑的推理（本部分随堂题目较难，大家不要灰心）",
                "045_有序对与笛卡儿积",
                "046_二元关系",
                "047_关系矩阵与关系图",
                "048_定义域、值域与像集",
                "049_关系的基本运算",
                "050_关系运算的性质",
                "051_关系的幂和道路（一）",
                "052_关系的幂和道路（二）",
                "053_关系的性质——自反性",
                "054_关系的性质——对称性",
                "055_关系的性质——传递性",
                "056_关系性质的判断",
                "057_关系运算对性质的保持",
                "058_扩展材料——特殊关系的计数",
                "059_关系的闭包",
                "060_关系闭包运算的性质",
                "061_沃舍尔算法",
                "062_沃舍尔算法纸上作业法",
                "063_划分",
                "064_等价关系、等价类和商集",
                "065_由划分构造等价关系",
                "066_由等价关系得到划分",
                "067_过河问题",
                "068_函数",
                "069_函数的性质",
                "070_函数的复合",
                "071_逆函数",
                "072_使用序列表示集合",
                "073_哈希函数",
                "074_地板函数与天花板函数",
                "075_置换",
                "076_轮换",
                "077_百囚犯问题（100prisonersproblem）",
                "078_不完美的“完美洗牌法”",
                "079_完美的“洗”牌",
                "080_集合的势",
                "081_偏序集",
                "082_积偏序与字典序",
                "083_哈斯图",
                "084_极大元与极小元",
                "085_最大元与最小元",
                "086_上界与下界",
                "087_上确界与下确界",
                "088_拓扑排序",
                "089_格",
                "090_信息流的安全格模型",
                "091_Chomp游戏",
                "092_无向图与有向图",
                "093_握手定理",
                "094_特殊的图",
                "095_图的同构",
                "096_子图与补图",
                "097_道路、回路与连通性",
                "098_图的矩阵表示",
                "099_欧拉道路与欧拉回路",
                "100_弗勒里算法",
                "101_哈密尔顿道路与哈密尔顿回路",
                "102_哈密尔顿图的刻画",
                "103_扩展内容 骑士巡游",
                "104_扩展内容 旅行商问题",
                "105_平面图",
                "106_欧拉公式",
                "107_对偶图",
                "108_扩展内容 库拉托夫斯基定理",
                "109_支配集",
                "110_点独立集",
                "111_点覆盖集",
                "112_图的着色",
                "113_韦尔奇 鲍威尔算法",
                "114_扩展内容 四色定理",
                "115_扩展内容 拉姆齐数",
                "116_顿时错乱游戏",
                "117_中国邮路问题",
                "118_抽芽游戏Sprouts",
                "119_抱子甘蓝游戏（Brusselssprouts）",
                "120_无向树",
                "121_最小支撑树",
                "122_克鲁斯卡算法",
                "123_普里姆算法",
                "124_破圈法",
                "125_博鲁夫卡算法",
                "126_扩展内容 最小瓶颈支撑树和斯坦纳树",
                "127_单源最短道路",
                "128_根树",
                "129_双链树",
                "130_二叉位置树的遍历",
                "131_标号树",
                "132_前缀码",
                "133_霍夫曼编码",
                "134_运算",
                "135_二元运算的性质",
                "136_半群与亚群",
                "137_群",
                "138_群的同态与同构",
                "139_群的性质",
                "140_子群",
                "141_循环群与置换群",
                "142_陪集与拉格朗日定理",
                "143_正规子群与商群",
                "144_环与域",
                "145_伯恩赛德引理与波利亚定理",
                "146_麻花辫",
                "147_魔方",
                "148_匹配",
                "149_伯奇引理",
                "150_最大匹配构造算法",
                "151_霍尔定理",
                "152_拉丁方",
                "153_边覆盖集",
                "154_柯尼希 艾盖尔瓦里定理",
                "155_二部图中的最佳匹配",
                "156_流网络",
                "157_剩余图和可增广道路",
                "158_最大流最小割定理",
                "159_福特·福尔克森最大流算法",
                "160_网络最大流的应用",
                "161_字母表与串",
                "162_形式语言",
                "163_文法",
                "164_文法的分类",
                "165_巴科斯 诺尔范式和语法图",
                "166_有限状态自动机",
                "167_非确定性有限状态自动机",
                "168_正则文法和有限状态自动机",
                "169_正则表达式",
                "170_林登麦伊尔系统",
                "171_格雷码",
                "172_格雷码与超立方图的哈密尔顿回路",
                "173_九连环与格雷码",
                "174_汉诺塔",
                "175_汉诺塔的非递归算法",
                "176_汉诺塔图",
                "177_汉诺塔与格雷码",
                "178_Hanoi塔玩具的变体",
                "179_欧几里得算法与裴蜀等式",
                "180_凑邮资问题",
                "181_分油问题",
                "182_登阶问题",
                "183_台球桌问题",
                "184_波利亚的果园",
                "185_正多面体",
                "186_存储器轮",
                "187_德·布鲁因有向图",
                "188_“德·布鲁因”魔术",
                "189_谢尔宾斯基三角形",
                "190_格路问题",
                "191_卡特兰数",
                "192_波利亚的醉汉",
                "193_应用鸽巢原理的纸牌魔术一则",
                "194_应用鸽巢原理的纸牌魔术又一则"
        };
        for (String newName : fileNames) {

            Iterator<File> iterator = newFiles.iterator();
            while (iterator.hasNext()) {
                File f = iterator.next();
                String oldNameRemover = f.getName().replaceAll("\\d+_","").replaceAll("\\..*","");
                String newNameRemover = newName.replaceAll("\\d+_", "");
                if (StringUtils.equals(oldNameRemover,newNameRemover)) {
                    renameFileName(newName+".mp4", f, f.getName());
                    iterator.remove();
                }
            }
        }
    }

    private static void renameFileName(String newName, File f, String oldName) {
        // 在当前文件夹下进行重命名处理
        String pathname = f.getParent() + File.separator + newName;
        File newFile = new File(pathname);
        f.renameTo(newFile);
        System.out.println("file rename finish, from {" + oldName + " } to {" + newName + "}");
    }

}
